<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <!-- default-expand-all -->
        <div class="head-container">
          <el-tree
            class="treeStyle"
            :data="deptOptions"
            :props="defaultProps"
            default-expand-all
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="案件类型" prop="caseType">
            <el-select
              v-model="queryParams.caseType"
              placeholder="请选择案件类型"
              clearable
            >
              <el-option
                v-for="dict in dict.type.jl_case_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="案件名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入案件名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="案件日期" prop="date">
            <el-date-picker
              clearable
              v-model="queryParams.date"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择案件日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['cases:cases:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['cases:cases:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['cases:cases:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['cases:cases:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="casesList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="id" />
          <el-table-column label="案件类型" align="center" prop="caseType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.jl_case_type"
                :value="scope.row.caseType"
              />
            </template>
          </el-table-column>
          <el-table-column label="案件名称" align="center" prop="name" />
          <el-table-column label="部门" align="center" prop="deptName" />
          <el-table-column label="上报人" align="center" prop="nickName" />
          <el-table-column
            label="案件日期"
            align="center"
            prop="date"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.date, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="上报时间" align="center" prop="createTime" />
          <el-table-column
            label="案件内容"
            align="center"
            prop="content"
            v-if="false"
          />
          <el-table-column
            label="案件视频"
            align="center"
            prop="videos"
            v-if="false"
          />
          <el-table-column
            label="案件文件"
            align="center"
            prop="files"
            v-if="false"
          />
          <el-table-column
            label="案件照片"
            align="center"
            prop="images"
            width="100"
            v-if="false"
          >
            <template slot-scope="scope">
              <image-preview :src="scope.row.images" :width="50" :height="50" />
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            v-if="false"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleDetail(scope.row)"
                >查看</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['cases:cases:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['cases:cases:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改案件管理对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="900px"
      append-to-body
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="案件类型" prop="caseType">
              <el-select v-model="form.caseType" placeholder="请选择案件类型">
                <el-option
                  v-for="dict in dict.type.jl_case_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案件名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入案件名称"
                :readonly="isdisable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="案件日期" prop="date">
              <el-date-picker
                clearable
                v-model="form.date"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择案件日期"
                :disabled="isdisable"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案件内容" prop="content">
              <el-input
                v-model="form.content"
                type="textarea"
                placeholder="请输入内容"
                :readonly="isdisable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="案件视频" prop="videos">
          <el-button
            size="mini"
            v-show="showButton"
            type="primary"
            @click="ChooseVideo = true"
            >选取视频</el-button
          >
          <uploader-video
            v-if="ChooseVideo"
            @videosListData="videosListData"
            @videoData="videoData"
            ref="uploaderVideo"
          />
          <file-upload-video
            v-model="form.videos"
            :isShowTip="!isdisable"
            :isDisabled="isdisable"
            :fileDownLoadPath="videoDownFilePath"
            :fileUpLoadPath="upLoadPath"
            :limit="100"
            :fileSize="10000"
          />
        </el-form-item>
        <el-form-item label="案件文件" prop="files">
          <file-upload
            v-model="form.files"
            :isShowTip="!isdisable"
            :isDisabled="isdisable"
            :fileDownLoadPath="downFilePath"
            :fileUpLoadPath="upLoadPath"
            :limit="100"
            :fileSize="1000"
          />
        </el-form-item>
        <el-form-item label="案件照片" prop="images">
          <image-upload
            v-model="form.images"
            :isShowTip="!isdisable"
            :limit="100"
            :fileSize="1000"
            :isDisabled="isdisable"
          />
        </el-form-item>
        <el-form-item label="案件录音" prop="audios">
          <file-upload-audio
            v-model="form.audios"
            :isShowTip="!isdisable"
            :isDisabled="isdisable"
            :fileDownLoadPath="videoDownFilePath"
            :fileUpLoadPath="upLoadPath"
            :limit="100"
            :fileSize="1000"
            :fileType="audio_types"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
            :readonly="isdisable"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isdisable"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCases,
  getCases,
  delCases,
  addCases,
  updateCases,
} from "@/api/cases/cases";
import axios from "axios";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Cases",
  dicts: ["jl_case_type"],
  components: { Treeselect },
  data() {
    return {
      //选取视频
      showButton: true,
      //添加视频选择框
      ChooseVideo: false,
      videoDownFilePath: "jlcase/",
      // 音频文件格式
      audio_types: ["swf", "flv", "mp3", "wav", "wma", "m4a", "amr"],
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      downFilePath: "/common/download?fileName=",
      upLoadPath: "/common/upload",
      // 禁用表单
      isdisable: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 案件管理表格数据
      casesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        date: null,
        deptId: null,
        caseType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "案件名称不能为空", trigger: "blur" },
        ],
        date: [
          { required: true, message: "案件日期不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 查询案件管理列表 */
    getList() {
      this.loading = true;
      listCases(this.queryParams).then((response) => {
        this.casesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      if (this.title == "修改案件") {
        this.delvideoData();
      }
      this.open = false;
      this.reset();
      //关闭添加视频框
      this.ChooseVideo = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        date: null,
        content: null,
        videos: "",
        files: null,
        images: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        caseType: null,
        audios: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.deptId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.showButton = true;
      this.isdisable = false;
      this.open = true;
      this.title = "添加案件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.showButton = true;
      this.reset();
      this.isdisable = false;
      const id = row.id || this.ids;
      getCases(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改案件";
      });
    },
    /** 查看 */
    handleDetail(row) {
      this.showButton = false;
      this.reset();
      this.isdisable = true;
      const id = row.id || this.ids;
      getCases(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看案件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.ChooseVideo == true) {
        this.$refs.uploaderVideo.relativePathList();
      }
      this.$confirm("是否确定上述内容已经填写且上传完成?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$refs["form"].validate((valid) => {
            if (valid) {
              if (this.form.id != null) {
                updateCases(this.form).then((response) => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                addCases(this.form).then((response) => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          });
        })
        .catch(() => {});
      //关闭添加视频框
      this.ChooseVideo = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除案件编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCases(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cases/cases/export",
        {
          ...this.queryParams,
        },
        `cases_${new Date().getTime()}.xlsx`
      );
    },
    //子组件点击确定后执行的命令
    videosListData(val) {
      if (this.form.videos === "") {
        this.form.videos = val;
      } else {
        this.form.videos = this.form.videos.split(",");
        this.form.videos.push(val);
        this.form.videos = this.form.videos.toString();
      }
      this.ChooseVideo = false;
    },
    handleClose(done) {
      if (this.title == "查看案件") {
        this.delvideoData();
        this.ChooseVideo = false;
        done();
      } else {
        this.$confirm("确认关闭？")
          .then((_) => {
            this.delvideoData();
            this.ChooseVideo = false;
            done();
          })
          .catch((_) => {});
      }
    },
    videoData(val) {
      // console.log(val);
      this.videoDataList = val;
    },
    //上传中途关闭
    delvideoData() {
      // console.log(610, this.videoDataList)
      axios.get(
        process.env.VUE_APP_BASE_API +
          "/upload/delRedisKey/" +
          this.videoDataList,
        {
          headers: { Authorization: "Bearer " + getToken() },
        }
      );
    },
  },
};
</script>
